package demo;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polyline;
import javafx.stage.Stage;

// 简单的鼠标点击画图程序
public class PolylinePainter extends Application {
	private Pane root = new Pane();
	private Polyline polyline;		// 折线
	
	public static void main(String[] args) {
		Application.launch(args);
	}
	
	@Override
	public void start(Stage stage) {
		
		polyline = new Polyline();
		polyline.setStroke(Color.DODGERBLUE);
		ObservableList<Double> list = polyline.getPoints();
		root.getChildren().add(polyline);
		
		Scene scene = new Scene(root, 400, 300);
		stage.setScene(scene);
		stage.show();
		
		// 事件
		root.setOnMouseClicked(e -> {
			if(e.getButton() == MouseButton.PRIMARY) {          // 左键画图
				list.addAll(e.getX(), e.getY());
			} else if(e.getButton() == MouseButton.SECONDARY) {   // 右键撤销
				list.remove(list.size()-1);
				list.remove(list.size()-1);
			}
		});
	}
}
